using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Default webpage
/// </summary>
public partial class DefaultASPX : System.Web.UI.Page {

	/// <summary>
	/// Output to the webpage
	/// </summary>
	protected System.Web.UI.WebControls.Literal pageOutput;

	/// <summary>
	/// Page load event, just redirect in this OWED implementation
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e)
	{
		// for this iteration, just goto viewdocument list
		Response.Redirect("ViewDocumentList.aspx");
		System.Net.WebClient wc;
		byte[] response;
		BinaryWriter binWriter;
		System.Diagnostics.ProcessStartInfo PI;
		System.Diagnostics.Process p;

		System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

		//someVal.Text = "--output " + Request.QueryString["document"] + ".html --stringparam use.extensions 0 docbook-xsl/xhtml/docbook.xsl " + Request.QueryString["document"] + ".xml";
		try {
			if (Request.QueryString["type"] == null) {
				Request.QueryString["type"] = "xhtml";
			}
		} catch (Exception) { }

		try {
			if (Request.QueryString["document"] == null) {
				Request.QueryString["document"] = "owed";
			}
		} catch (Exception) { }

		if (!File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + @"\cache\" + Request.QueryString["type"] + @"\" + Request.QueryString["document"])) {
			//binWriter = new BinaryWriter(File.Open("./cache/" + Request.QueryString["type"] + "/" + Request.QueryString["document"], FileMode.Create));
			// not in cache, fetch out
			if (Request.QueryString["type"] == "pdf") {
				wc = new System.Net.WebClient();
				response = wc.DownloadData("http://my.student.iastate.edu/pdf.php?filename=" + Request.QueryString["document"]);
				//binWriter.Write(response);
			}

			if (Request.QueryString["type"] == "rtf") {
				wc = new System.Net.WebClient();
				response = wc.DownloadData("http://my.student.iastate.edu/rtf.php?filename=" + Request.QueryString["document"]);
				//binWriter.Write(response);
			}

			if (Request.QueryString["type"] == "xhtml") {
				System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory + @"\bin");
				PI = new System.Diagnostics.ProcessStartInfo();
				PI.FileName = System.AppDomain.CurrentDomain.BaseDirectory + @"\bin\" + "xsltproc.exe";
				PI.Arguments = "--output ../cache/" + Request.QueryString["type"] + "/" + Request.QueryString["document"] + " --stringparam use.extensions 0 docbook-xsl/xhtml/docbook.xsl " + Request.QueryString["document"] + ".xml";
				p = new System.Diagnostics.Process();
				p.StartInfo = PI;
				p.Start();
				p.WaitForExit();
			}
		}

		System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory + @"\cache\" + Request.QueryString["type"]);
		FileStream file = new FileStream(Request.QueryString["document"], FileMode.Open, FileAccess.Read);
		StreamReader sr = new StreamReader(file);
		string s = sr.ReadToEnd();
		sr.Close();
		file.Close();
		pageOutput.Text = s;
	}
}
